const { User } = require("../../model/User");
const bcrypt = require('bcrypt');

module.exports = async (req, res, next) => {
    const { password, username, email, role, state } = req.body;
    const { id } = req.query;
    let user = await User.findOne({_id: id})
    const isValid = await bcrypt.compare(password, user.password)
    if (isValid) {
        // 密码比对成功
        await User.updateOne({_id: id}, {
            username: username,
            email: email,
            role: role,
            state: state,
        })
        return res.redirect('/admin/user');
    } else {
        // 密码比对失败
        let obj = {path: '/admin/user-edit', message: "密码比对失败，不能进行用户信息的修改", id: id}
        next(JSON.stringify(obj));
        return;
    }

}